﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace MingYuanyun.WebApi.Abstractions
{
    // public 方法，swagger 会当作一个 接口，故必须 protected

    public abstract class BasicController : Controller
    {
        #region object result

        /// <summary>
        /// StatusCodes.Status200OK
        /// </summary>
        //protected HttpRequestResult HttpRequestResult(object data = null, bool success = false, string message = null)
        //{
        //    return StatusCodeHttpRequestResult(StatusCodes.Status200OK, data, success, message);
        //}

        protected HttpRequestResult OkHttpRequestResult(object data = null)
        {
            return StatusCodeHttpRequestResult(StatusCodes.Status200OK, data, true, string.Empty);
        }

        protected HttpRequestResult ErrorHttpRequestResult(string message)
        {
            return StatusCodeHttpRequestResult(StatusCodes.Status200OK, null, false, message);
        }

        protected HttpRequestResult BadHttpRequestResult(object data = null, string message = null)
        {
            return StatusCodeHttpRequestResult(StatusCodes.Status400BadRequest, data, false, message);
        }

        private HttpRequestResult StatusCodeHttpRequestResult(int statusCode, object data, bool success, string message)
        {
            Response.StatusCode = statusCode;
            return new HttpRequestResult(data, success, message);
        }

        #endregion

        #region generic result

        /// <summary>
        /// StatusCodes.Status200OK
        /// </summary>
        //protected HttpRequestResult<T> HttpRequestResult<T>(T data = default(T), bool success = false, string message = null)
        //{
        //    return StatusCodeHttpRequestResult(StatusCodes.Status200OK, data, success, message);
        //}

        protected HttpRequestResult<T> OkHttpRequestResult<T>(T data = default(T))
        {
            return StatusCodeHttpRequestResult(StatusCodes.Status200OK, data, true, string.Empty);
        }

        protected HttpRequestResult<T> ErrorHttpRequestResult<T>(string message)
        {
            return StatusCodeHttpRequestResult<T>(StatusCodes.Status200OK, default(T), false, message);
        }

        protected HttpRequestResult<T> BadHttpRequestResult<T>(T data = default(T), string message = null)
        {
            return StatusCodeHttpRequestResult(StatusCodes.Status400BadRequest, data, false, message);
        }

        protected HttpRequestResult<T> StatusCodeHttpRequestResult<T>(int statusCode, T data, bool success, string message)
        {
            Response.StatusCode = statusCode;

            return new HttpRequestResult<T>(data, success, message);
        }

        #endregion
    }
}
